<template>
  <div class="app-container">
    <el-form :inline="true" ref="searchForm" :model="listQuery" class="demo-form-inline">
      <el-form-item>
        <el-button class="btn-add" @click="addProductAttr()" size="small"> 添加 </el-button>
      </el-form-item>
    </el-form>

    <div class="table-container">
      <el-table
        ref="productAttrTable"
        :data="list"
        height="100%"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="属性名称" minWidth="140" align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="商品类型" width="140" align="center">
          {{ $route.query.cname }}
        </el-table-column>
        <el-table-column label="属性是否可选" width="120" align="center">
          <template #default="scope">{{ selectTypeFilter(scope.row.selectType) }}</template>
        </el-table-column>
        <el-table-column label="属性值的录入方式" width="150" align="center">
          <template #default="scope">{{ inputTypeFilter(scope.row.inputType) }}</template>
        </el-table-column>
        <el-table-column label="可选值列表" minWidth="180" show-overflow-tooltip align="center">
          <template #default="scope">{{ scope.row.inputList }}</template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template #default="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template #default="scope">
            <el-button link size="small" @click="handleUpdate(scope.row)">编辑 </el-button>
            <el-button link size="small" type="danger" @click="handleDelete(scope.row)">删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option v-for="item in operates" :key="item.value" :label="item.label" :value="item.value"> </el-option>
      </el-select>
      <el-button class="search-button" @click="handleBatchOperate()" type="primary" size="small"> 确定 </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        v-model:current-page="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script setup lang="ts">
import { fetchList, deleteProductAttr } from '@/api/productAttr'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'
import type { ProductAttr, ProductAttrPageDto } from '@/models/productAttr'

defineOptions({ name: 'ProductAttrList' })
const route = useRoute()
const router = useRouter()
const list = ref<ProductAttr[]>([])
const total = ref(0)
const listLoading = ref(true)
const listQuery = ref<ProductAttrPageDto>({ pageNum: 1, pageSize: 10, type: Number(route.query.type) })
const operateType = ref(null)
const multipleSelection = ref<ProductAttr[]>([])
const operates = ref([{ label: '删除', value: 'deleteProductAttr' }])
async function getList() {
  listLoading.value = true
  const response = await fetchList(Number(route.query.cid), listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
getList()
function addProductAttr() {
  router.push({
    path: '/pms/productAttr/upsert',
    query: { cid: route.query.cid, type: route.query.type },
  })
}
function handleSelectionChange(val: ProductAttr[]) {
  multipleSelection.value = val
}
function handleBatchOperate() {
  if (multipleSelection.value.length < 1) {
    ElMessage.warning('请选择一条记录')
    return
  }
  if (operateType.value !== 'deleteProductAttr') {
    ElMessage.warning('请选择批量操作类型')
    return
  }
  multipleSelection.value && handleDeleteProductAttr(multipleSelection.value.map((item) => item.id).join(','))
}
function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}
function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}
function handleUpdate(row: ProductAttr) {
  router.push({ path: '/pms/productAttrUpsert', query: { id: row?.id } })
}
async function handleDeleteProductAttr(ids: string) {
  await ElMessageBox.confirm('是否要删除该属性', '提示', { type: 'warning' })
  await deleteProductAttr({ ids })
  ElMessage.success('删除成功')
  getList()
}
function handleDelete(row: ProductAttr) {
  handleDeleteProductAttr(row?.id + '')
}
function inputTypeFilter(value: number) {
  if (value === 1) {
    return '从列表中选取'
  } else {
    return '手工录入'
  }
}
function selectTypeFilter(value: number) {
  if (value === 1) {
    return '单选'
  } else if (value === 2) {
    return '多选'
  } else {
    return '唯一'
  }
}
</script>

<style lang="scss" scoped></style>
